package cn.com.duiba.millionaire.center.biz.dao.impl;

import cn.com.duiba.millionaire.center.biz.constants.DatabaseSchema;
import cn.com.duiba.millionaire.center.biz.dao.BaseDao;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireWithdrawDao;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireWithdrawEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoxuda on 2018/1/31.
 */
@Repository
public class MillionaireWithdrawDaoImpl extends BaseDao implements MillionaireWithdrawDao {
    @Override
    public long insert(MillionaireWithdrawEntity entity) {
        if(StringUtils.length(entity.getEnvUa()) > 255){
            entity.setEnvUa(entity.getEnvUa().substring(0, 255));
        }
        insert("insert", entity);
        return entity.getId();
    }

    @Override
    public MillionaireWithdrawEntity findById(long id) {
        return selectOne("findById", id);
    }

    @Override
    public List<MillionaireWithdrawEntity> batchFindByConsumerId(long consumerId) {
        return selectList("batchFindByConsumerId", consumerId);
    }

    @Override
    public Long countWithdrawAmount(Long consumerId) {
        return selectOne("countWithdrawAmount", consumerId);
    }

    @Override
    public boolean update2UserPayed(long id) {
        return update("update2UserPayed", id) == 1;
    }

    @Override
    public boolean update2DevPayed(long id) {
        return update("update2DevPayed", id) == 1;
    }

    @Override
    public boolean update2Success(long id) {
        return update("update2Success", id) == 1;
    }

    @Override
    public boolean update2Fail(long id, String failMsg) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", id);
        param.put("failMsg", failMsg);
        return update("update2Fail", param) == 1;
    }

    @Override
    protected DatabaseSchema chooseSchema() {
        return DatabaseSchema.MILLIONAIRE_CONFIG;
    }
}
